/*jslint sloppy: true */
/*global require */
var angular = require('angular'),
	probe = require('../global');
require('./open.html');

probe.controller('OpenCase', function (ocDI, ocData, Case, $scope) {
	$scope.caseList = Case.list(function (caseList) {
		$scope.tagTable = caseList.reduce(function (table, caseObj) {
			angular.forEach(caseObj.tags, function (tag) {
				table[tag] = false;
			});

			return table;
		}, {});
	});

	$scope.watchCase = function ($case) {
		$scope.caseData = $case;
	};
	$scope.reviewedTypeDisabled = false;
	$scope.keyWord = '';
	$scope.isReviewed = '';
	var tagsFilterArr = [];
	$scope.tagsFilterFn = function ($case) {
		if (tagsFilterArr.length === 0) {
			return true;
		}
		
		return tagsFilterArr.every(function (tag) {
			return $case.tags.indexOf(tag) !== -1;
		});
	};

	$scope.open = function () {
		(ocData.fn || angular.noop)($scope.caseData);
		ocDI.closeDialog();
	};

	//init
	(ocData.preOpenCase || angular.noop)($scope);
	$scope.tagTable = {};

	$scope.$watchCollection('tagTable', function () {
		tagsFilterArr.length = 0;
		angular.forEach($scope.tagTable, function (checked, tag) {
			if (checked) {
				tagsFilterArr.push(tag);
			}
		});
	});
});
